package com.zp.crm.Controller.DdController;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.gson.Gson;
import com.zp.crm.entity.DdEntity.DdEntity;
import com.zp.crm.service.DdService.DdService;

@Controller
@RequestMapping("/Dd")
public class DdController {
	
	@Resource
	public DdService ddService;
	
	@RequestMapping(value="/select",method=RequestMethod.POST)
	public void selectCode(HttpServletRequest request,HttpServletResponse response){
		response.setContentType("text/html;charset=utf-8");
		String code=request.getParameter("code");
		String pagestr=request.getParameter("page");
		int page=1,size=10;
		if(pagestr != null){
			page=Integer.parseInt(pagestr);
		}
		Map<String, Object> map=ddService.selectCode(page,size,code);
		try{
			PrintWriter out=response.getWriter();
			out.println(map.get("message"));
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@RequestMapping(value="/insert",method=RequestMethod.POST)
	public void Insert(HttpServletRequest request,HttpServletResponse response) throws NumberFormatException, ParseException{
		response.setContentType("text/html;charset=utf-8");
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
		String code=request.getParameter("code");
		String name=request.getParameter("name");
		String ddtime=request.getParameter("ddtime");
		String dddz=request.getParameter("dddz");
		String ddstuts=request.getParameter("ddstuts");
		String ddje=request.getParameter("ddje");
		String ddcreateuser=request.getParameter("user");
		DdEntity entity=new DdEntity(code, name, f.parse(ddtime), dddz, ddstuts, Float.parseFloat(ddje), ddcreateuser, new Date());
		Map<String, Object> map=ddService.insert(entity);
		try{
			PrintWriter out=response.getWriter();
			String json=new Gson().toJson(map);
			out.println(json);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
